﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.Data;
using FrienDev.BusinessRules;

public partial class Controls_Activity_State : System.Web.UI.UserControl
{

    private int m_activityID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request["activityid"] != null)
        {
            int activityID = 0;
            int.TryParse(Request["activityid"], out activityID);
            m_activityID = activityID;
            int stateNum = ActivityRules.Instance.GetActivityState(activityID);
            if (stateNum >= 0 && stateNum <= 5)
            {
                if (stateNum >= 1 && stateNum <= 5)
                {
                    signUpHyperLink.Visible = false;
                }
                state.Text = ActivityRules.Instance.GetStateDescription(stateNum);
                string userState = ActivityRules.Instance.GetUserActivityState(UserRules.Instance.GetCurrentUserId(), m_activityID);
                //signUpHyperLink.Click += new EventHandler(btnSignUpClick);
                if (userState == "promoter")
                {
                    signUpHyperLink.Visible = false;
                }
                else if (userState == "" && !ActivityRules.Instance.IsActivitySignUpFinished(m_activityID) && !ActivityRules.Instance.IsActivitySignUpNumExceeded(m_activityID))
                {
                    signUpHyperLink.Visible = true;
                    img_SignUpIcon.Visible = true;
                    signUpHyperLink.Text = "我要报名";
                    //if (ActivityRules.Instance.GetActivityInfo(m_activityID).IsNeedPermission == true)
                    //{
                    //    signUpHyperLink.OnClientClick = "javascript: alert('已成功报名该活动,等待管理员审核!');";
                    //}
                    //else
                    //{
                    //    signUpHyperLink.OnClientClick = "javascript: alert('已成功报名该活动!');";
                    //}
                }
                else if ((userState == "candidate" || userState == "joined") && stateNum < 4)
                {
                    signUpHyperLink.Visible = true;
                    signUpHyperLink.Text = "我要退出";
                    signUpHyperLink.OnClientClick = "javascript: if (!confirm('你真的要退出么?')) return false;";
                }
                else if (stateNum >= 4 && userState == "joined")
                {
                    signUpHyperLink.Visible = true;
                    signUpHyperLink.Text = "填写反馈";
                }

                if (!ActivityRules.Instance.IsCurrentUserIsActivityPromoter(activityID) || stateNum == 2 || stateNum == 5)
                {
                    btnCancleActivity.Visible = false;
                }
                else
                {
                    btnCancleActivity.Visible = true;
                }
            }

        }
    }

    protected void btnSignUpClick(object sender, EventArgs e)
    {
        if ((sender as LinkButton).Text == "我要报名")
        {
            Response.Redirect("~/Activity/ActivitySignUp.aspx?activityid="+m_activityID);
            //User user = UserRules.Instance.GetCurrentUser();
            //if (user.Gender == false)
            //{
            //    ActivityRules.Instance.SignUpActivity(UserRules.Instance.GetCurrentUserId(), m_activityID, 0, 1);
            //}
            //else
            //{
            //    ActivityRules.Instance.SignUpActivity(UserRules.Instance.GetCurrentUserId(), m_activityID, 1, 0);
            //}
            //Response.Redirect(Request.Url.ToString());
        }
        else if ((sender as LinkButton).Text == "我要退出")
        {
            ActivityRules.Instance.QuitActivity(UserRules.Instance.GetCurrentUserId(), m_activityID);
            Response.Redirect(Request.Url.ToString());
        }
        else if ((sender as LinkButton).Text == "填写反馈")
        {
            Response.Redirect("~/Activity/ActivityFeedBackFill.aspx?activityId=" + m_activityID);
        }
    }

    protected void btnCancleActivity_Click(object sender, EventArgs e)
    {
        //TODO:加入确认对话框
        ActivityRules.Instance.UpdateActivityState(m_activityID, 5);
        Response.Redirect(Request.Url.ToString());
    }
}

